﻿Imports Send2Twitter
Imports ActiveWinamp
Imports System.Collections.Specialized
Imports System.ComponentModel.Composition

<ExportCompent(GetType(IInfoProvider), "Winamp With Ax", "4f3a51e9-8a2f-421a-a086-0ffba6322cd2", "1.0.0.1")>
Public Class iTunesInfoGen
    Inherits InfoProviderBase

    Protected WinampApp As Application

    Public Overrides Function GetAvailability() As Boolean
        If Not IsWinampStarted() Then Return False
        If WinampApp Is Nothing Then Return False
        Return WinampApp.PlayState = 1
    End Function

    Public Overrides Function GetInfo() As Dictionary(Of String, String)
        Dim r As New Dictionary(Of String, String)()
        If WinampApp Is Nothing Then
            If IsWinampStarted() Then
                StartUp()
            Else
                Return r
            End If
        End If
        Dim t = WinampApp.Playlist.Item(WinampApp.Playlist.Position)
        On Error Resume Next
        r.Add("album", t.Album)
        r.Add("albumartist", t.ATFString("[$if2(%albumartist%,%artist%)]"))
        r.Add("artist", t.Artist)
        r.Add("bitrate", t.ATFString("[%bitrate%]"))
        r.Add("filetype", t.ATFString("[$fileext(%filename%)]"))
        r.Add("genre", t.Genre)
        r.Add("index", WinampApp.Position.ToString)
        r.Add("title", t.Title)
        r.Add("playedcount", t.Playcount.ToString)
        r.Add("rating", t.Rating.ToString)
        r.Add("samplerate", t.ATFString("[%samplerate%]"))
        r.Add("track", t.Track.ToString)
        r.Add("year", t.ATFString("[%year%]"))
        On Error GoTo 0
        Return r
    End Function

    Public Overrides Sub ShutDown()
        If WinampApp IsNot Nothing Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(WinampApp)
        End If
        WinampApp = Nothing
    End Sub

    Public Overrides Sub StartUp()
        If IsWinampStarted() Then
            WinampApp = New Application()
        Else
            WinampApp = Nothing
        End If
    End Sub

    Private Function IsWinampStarted() As Boolean
        If Process.GetProcessesByName("Winamp").Length > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
End Class
